<?php
define("RED", "r");
define("BLUE", "b");
define("GREEN", "g");
define("YELLOW", "y");

define("Reverse", 4);
define("Stop", 3);
define("WildDraw", 2);
define("Draw2", 1);

include_once("CardState.php");
include_once("ICard.php");
include_once("BaseCard.php");
include_once("NumberCard.php");
include_once("WildCard.php");
include_once("WildDrawCard.php");
include_once("Draw2Card.php");
include_once("StopCard.php");
include_once("ReverseCard.php");

function getCard($card, $para1 = "")
{
	if($card == "w4")
		return new WildDrawCard($para1);
	else if($card == "w0")
		return new WildCard($para1);
	else
	{
		$s1 = substr($card,0,1);
		$s2 = substr($card,-1);
			
		if($s2 == "d")
		return new Draw2Card($s1);
		else if($s2 == "r")
		return new ReverseCard($s1);
		else if($s2 == "s")
		return new StopCard($s1);
		else
		{
			return new NumberCard($s1,$s2);
		}
	}
}

function getNextPlayer($cardState,$tb)
{
	$nextSeatID = $tb->currentPlayerSeatID;
	if($cardState->direction > 0)
	{
		for($i = 0; $i < $cardState->step; $i++)
		{
			$nextSeatID = $tb->seatList[$nextSeatID]->nextSeatID;
		}
	}
	else
	{
		for($i = 0; $i < $cardState->step; $i++)
		{
			$nextSeatID = $tb->seatList[$nextSeatID]->prevSeatID;
		}
	}
	
	return $nextSeatID;
}

function getPunishCard($table,$user)
{
	$pcard = array();
	for($i = 0; $i < $table->cardState->num; $i++)
	{
		$c1 = $table->popCard();
		array_push($pcard, $c1);
		array_push($table->seatList[$user->seatID]->cardList,$c1);
	}
	return $pcard;
}

/**
 * 返回当前状态的上一个玩家
 * */
function getPrevPlayer($tb)
{
	$cardState = $tb->cardState;
	$nextSeatID = $tb->currentPlayerSeatID;
	if($cardState->direction > 0)
	{
		for($i = 0; $i < $cardState->step; $i++)
		{
			$nextSeatID = $tb->seatList[$nextSeatID]->prevSeatID;
		}
	}
	else
	{
		for($i = 0; $i < $cardState->step; $i++)
		{
			$nextSeatID = $tb->seatList[$nextSeatID]->nextSeatID;
		}
	}
	
	return $nextSeatID;
}
